package com.lanyou.cook.entity.domain;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.entitylistener.domain.DBlinkInterfaceEntityListenerForLog;

@Entity
@EntityListeners({ AuditingEntityListener.class,DBlinkInterfaceEntityListenerForLog.class})
@Table(name = "T_COOK_BU_DBLINK_IF_REL")
public class DBlinkInterface{
	
	private String id;
	//dblink接口名
	private String name;
	
	private int version;
	
	private FieldSystem fieldSystem;
	
	private DBlink dblink;
	
	private String remark;
	
	private List<DBlinkInterfaceColumnDefinition> dBlinkInterfaceColumnDefinitions;
	
	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "DBLINK_IF_REL_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name="DBLINK_IF_REL_NAME",length=50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	@ManyToOne
	@JoinColumn(name = "FIELD_SYSTEM_ID")
	public FieldSystem getFieldSystem() {
		return fieldSystem;
	}

	public void setFieldSystem(FieldSystem fieldSystem) {
		this.fieldSystem = fieldSystem;
	}
	
	@ManyToOne
	@JoinColumn(name = "T_COOK_BU_DBLINK_ID")
	public DBlink getDblink() {
		return dblink;
	}

	public void setDblink(DBlink dblink) {
		this.dblink = dblink;
	}
	@Column(name="REMARK",length=100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@CreatedBy
	@Column(length = 50,updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@OneToMany(mappedBy = "dblinkInterface")
	public List<DBlinkInterfaceColumnDefinition> getdBlinkInterfaceColumnDefinitions() {
		return dBlinkInterfaceColumnDefinitions;
	}

	public void setdBlinkInterfaceColumnDefinitions(List<DBlinkInterfaceColumnDefinition> dBlinkInterfaceColumnDefinitions) {
		this.dBlinkInterfaceColumnDefinitions = dBlinkInterfaceColumnDefinitions;
	}

	@Transient
	public String getFieldSystemName() {
		try {
			return (String) PropertyUtils.getProperty(fieldSystem, "name");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getFieldSystemId() {
		try {
			return (String) PropertyUtils.getProperty(fieldSystem, "id");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getFieldName() {
		try {
			return (String) PropertyUtils.getProperty(fieldSystem, "field.name");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getFieldId() {
		try {
			return (String) PropertyUtils.getProperty(fieldSystem, "field.id");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getDBlinkName() {
		try {
			return (String) PropertyUtils.getProperty(dblink, "name");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getDBlinkId() {
		try {
			return (String) PropertyUtils.getProperty(dblink, "id");
		} catch (Exception e) {
			return null;
		}
	}
	public static Specification<DBlinkInterface> idSpecification(String id){
		return new Specification<DBlinkInterface>(){
			@Override
			public Predicate toPredicate(Root<DBlinkInterface> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	
}